/*
 * GoodsDetailValueProvider.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.response.GoodsDetailValueResponse;
import com.yidiandao.business.ui.adapter.GoodsDetailValueImageAdapter;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 商品详情 商品评价 Item Provider
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/2 14:09
 */
public class GoodsDetailValueProvider
        extends ItemViewProvider<
        GoodsDetailValueResponse.ValueBean, GoodsDetailValueProvider.ViewHolder> {

    private Activity mActivity;

    public GoodsDetailValueProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.item_goods_detail_value, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(
            @NonNull ViewHolder holder, @NonNull GoodsDetailValueResponse.ValueBean data) {

        //头像
        ImageLoader.loadAvatar(mActivity, holder.mIvAvatar);
        //用户名
        holder.mTvNickname.setText(data.getNickname());
        //时间
        holder.mTvTime.setText(
                TimeUtils.format(Long.parseLong(data.getAddTime()) * 1000, "yyyy-MM-dd"));
        //评分
        holder.mRatingBar.setRating(Float.parseFloat(data.getGoodsRank()));
        //评语
        holder.tvDesc.setText(data.getMessage());

        //图片
        if (data.getPicture() != null && data.getPicture().size() != 0) {
            holder.mRecyclerView.setVisibility(View.VISIBLE);
            //设置横向
            LinearLayoutManager manager =
                    new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false);
            holder.mRecyclerView.setLayoutManager(manager);
            GoodsDetailValueImageAdapter adapter = new GoodsDetailValueImageAdapter(mActivity);
            List<List<String>> lists = new ArrayList<>();
            for (int i = 0; i < data.getPicture().size(); i++) {
                lists.add(data.getPicture());
            }
            adapter.setImages(lists);
            holder.mRecyclerView.setAdapter(adapter);
        } else {
            holder.mRecyclerView.setVisibility(View.GONE);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView mIvAvatar;
        private TextView mTvNickname;
        private TextView mTvTime;
        private RatingBar mRatingBar;
        private RecyclerView mRecyclerView;
        private TextView tvDesc;

        private ViewHolder(View itemView) {
            super(itemView);
            mIvAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            mTvNickname = (TextView) itemView.findViewById(R.id.tv_nickname);
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time);
            mRatingBar = (RatingBar) itemView.findViewById(R.id.rating_bar);
            tvDesc = (TextView) itemView.findViewById(R.id.tv_desc);
            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view);
        }
    }
}
